package com.bjsxt.usersCenter.common.util;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component("springEmailUtil")
public class SpringEmailUtil
{
	@Resource
	private JavaMailSender javaMailSenderImpl;
	@Resource
	private SimpleMailMessage simpleMailMessage;
	
	/**
	 * 发送邮件
	 */
	
	public boolean sendMail(String to,String subject,String text)
	{
		
		try
		{
			this.simpleMailMessage.setTo(to);
			this.simpleMailMessage.setSubject(subject);
			this.simpleMailMessage.setText(text);
			
			this.javaMailSenderImpl.send(this.simpleMailMessage);
			return true;
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.info("--发送邮件出错--",e);
		}
		
		
		
		return false;
	}
	public boolean sendMailHtml(String to,String subject,String text)
	{
		
		try
		{
			MimeMessage message=this.javaMailSenderImpl.createMimeMessage();
			/**
			 * true表示发送的是附文件
			 */
			MimeMessageHelper helper=new MimeMessageHelper(message,true,"UTF-8");
			
			helper.setFrom(this.simpleMailMessage.getFrom());
			helper.setTo(to);
			helper.setSubject(subject);
			/**
			 * true 表示附文件
			 */
			helper.setText(text,true);
			
			this.javaMailSenderImpl.send(message);
			return true;
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.info("--发送邮件出错--",e);
		}
		
		
		
		return false;
	}
	
	
	
}
